package be.pluyckx.batteryprogress.components.view;

import android.annotation.TargetApi;
import android.content.Context;
import android.content.res.TypedArray;
import android.os.Build;
import android.util.AttributeSet;
import android.view.Gravity;
import android.widget.RelativeLayout;
import android.widget.TextView;

import be.pluyckx.batteryprogress.R;

/**
 * Created by Philip on 19/08/13.
 */
public class DescriptionView extends RelativeLayout {
  private TextView mDefinition;
  private TextView mDescription;

  public DescriptionView(Context context) {
    super(context);

    init(null);
  }

  public DescriptionView(Context context, AttributeSet attrs) {
    super(context, attrs);

    init(attrs);
  }

  @TargetApi(Build.VERSION_CODES.HONEYCOMB)
  public DescriptionView(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);

    init(attrs);
  }

  private void init(AttributeSet attrs) {
    mDefinition = new TextView(getContext());
    mDescription = new TextView(getContext());
    mDefinition.setId(R.id.textview_definition);
    mDescription.setId(R.id.textview_description);
    float factor = getResources().getDisplayMetrics().density;
    mDefinition.setPadding(0, 0, (int) (5 * factor), 0);
    mDescription.setPadding((int) (5 * factor), 0, 0, 0);

    mDefinition.setSingleLine(true);
    RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
            LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
    params.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
    addView(mDefinition, params);

    params = new RelativeLayout.LayoutParams(
            RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
    params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
    params.addRule(RelativeLayout.RIGHT_OF, mDefinition.getId());
    mDescription.setGravity(Gravity.RIGHT);
    addView(mDescription, params);

    TypedArray a = getContext().obtainStyledAttributes(attrs, R.styleable.DescriptionView, 0, 0);
    try {
      setDefinition(a.getString(R.styleable.DescriptionView_definition));
      setDescription(a.getString(R.styleable.DescriptionView_description));
    } finally {
      a.recycle();
    }
  }

  public void setDefinition(String definition) {
    if (definition == null) {
      mDefinition.setText("");
    } else {
      mDefinition.setText(definition);
    }
  }

  public void setDescription(String description) {
    if (description == null) {
      mDescription.setText("");
    } else {
      mDescription.setText(description);
    }
  }


  public void setDefinition(int resId) {
    String str = getContext().getResources().getString(resId);
    mDefinition.setText(str);
  }

  public void setDescription(int resId) {
    String str = getContext().getResources().getString(resId);
    mDescription.setText(str);
  }
}
